

package com.tmall.testgen.plugin.views.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.TreeViewer;

import com.tmall.testgen.client.ClientDataCenter;
import com.tmall.testgen.common.bo.interact.back.InterceptInfo;
import com.tmall.testgen.common.bo.interact.back.syn.BaseInfo;
import com.tmall.testgen.common.util.StringUtil;
import com.tmall.testgen.plugin.views.GenCodeMonitorView;
import com.tmall.testgen.plugin.views.vo.TreeParent;

/**
 * 按顺序查看
* @author nianku haojia.hj@alibaba-inc.com
* @date 2014年7月10日
*
 */
public class CheckInterceptInfoByTimeAction extends Action {
	private TreeViewer viewer;
	private TreeParent interceptRoot;
	//private TreeParent mockRoot;
	
	public CheckInterceptInfoByTimeAction() {
		setText("时间维度查看");
		setToolTipText("时间维度查看");
		setImageDescriptor(ImageManeger.getImageDescriptor(ImageManeger.USED_CLEAR));
	}
	
	public void run() {
		GenCodeMonitorView.ischeckInterceptInfoByTime=true;
		
		List<InterceptInfo> infoes=ClientDataCenter.getInstance().getInfoes();
		if(infoes==null)
			infoes=new ArrayList<InterceptInfo>();
		
		List<InterceptInfo> interceptInfo=new ArrayList<InterceptInfo>();
		List<InterceptInfo> mockInfo=new ArrayList<InterceptInfo>();
		
		for(InterceptInfo info:infoes){
			if(info.getType()==BaseInfo.TYPE_INTERCEPT){
				interceptInfo.add(info);
			}else{
				mockInfo.add(info);
			}
				
		}
		
		interceptRoot.removeAllChildren();
		
		for(InterceptInfo info:interceptInfo){
			interceptRoot.addChild(info2tree(info));
		}
		
//		mockRoot.removeAllChildren();
//		
//		for(InterceptInfo info:mockInfo){
//			mockRoot.addChild(info2tree(info));
//		}
		viewer.refresh();
	}
	
	public void addInterceptInfo(InterceptInfo info){
		if(info.getType()==BaseInfo.TYPE_INTERCEPT){
			interceptRoot.addChild(info2tree(info));
		}else{
			//mockRoot.addChild(info2tree(info));
		}
		
		
		viewer.refresh();
	}
	
	/**
	 * 获取树形节点名称
	 * @return
	 */
	public static  String getNodeName(InterceptInfo info){
		return info.getClassName()+"."+info.getMethodName()+info.getMethodParamContent();
	}
	
	public static void addParamAndResultTree(TreeParent parent,InterceptInfo info){
		TreeParent importParam = new TreeParent("入参:");

		Map<String, Object>  paramMap=info.getParamMap();
		if(paramMap!=null){
			for(String name:paramMap.keySet()){
				TreeParent sub = new TreeParent(name+":    "+StringUtil.getTreeNodeName(paramMap.get(name)));
				sub.setLeafNode(true);
				sub.setValue(paramMap.get(name));
				importParam.addChild(sub);
			}
		}
		

		
		TreeParent exportParam = new TreeParent("出参:");

		Object obj = info.getResult();
		if(obj!=null){
			
			TreeParent sub = new TreeParent("result:    "+StringUtil.getTreeNodeName(obj));
			sub.setLeafNode(true);
			sub.setValue(obj);
			exportParam.addChild(sub);
		
			
		}

		parent.addChild(importParam);
		parent.addChild(exportParam);
	}
	public static TreeParent info2tree(InterceptInfo info) {
		TreeParent root = new TreeParent(getNodeName(info)+" ["+info.getExecuteTime()+"]");
		addParamAndResultTree(root,info);
		return root;
	}


	public TreeViewer getViewer() {
		return viewer;
	}

	public void setViewer(TreeViewer viewer) {
		this.viewer = viewer;
	}

	public TreeParent getInterceptRoot() {
		return interceptRoot;
	}

	public void setInterceptRoot(TreeParent interceptRoot) {
		this.interceptRoot = interceptRoot;
	}


	
	
}
